The WebViewController class allows you to display and interact with embedded web content inside your script. It is designed for use cases like custom in-app browsers, rendering dynamic HTML, or communicating with JavaScript running in a web context.
WebViewControllershouldAllowRequest?: (request) => Promise<boolean>An optional callback that determines whether to allow or block a request made by the WebView. This function is invoked before each resource is loaded, such as when navigating to a new page or submitting a form.
This is useful for intercepting navigation actions, implementing custom security logic, or filtering unwanted requests (e.g., ad domains).
The function receives a single request object with the following properties:
url: string
The full URL of the request.
method: string
The HTTP method (e.g., GET, POST).
body?: Data | null
Optional body data sent with the request (e.g., for POST requests).
headers: Record<string, string>
HTTP request headers.
timeoutInterval: number
The timeout interval in seconds for the request.
navigationType: "linkActivated" | "reload" | "backForward" | "formResubmitted" | "formSubmitted" | "other"
The context that triggered the navigation.
A Promise<boolean> resolving to:
true: allow the request to proceedfalse: block the requestloadURL(url: string): Promise<boolean>Loads a webpage by its URL.
Parameters:
url: The full URL of the webpage to load.Returns: Promise<boolean> — Resolves to true if the load succeeds.
loadHTML(html: string, baseURL?: string): Promise<boolean>Loads a web page using raw HTML content.
Parameters:
html: The HTML string to render.baseURL (optional): A base URL to resolve relative paths.Returns: Promise<boolean> — Resolves to true on successful load.
loadData(data: Data, mimeType: string, encoding: string, baseURL: string): Promise<boolean>Loads web content from raw data.
Parameters:
data: The binary content to load.mimeType: MIME type of the content (e.g., "text/html").encoding: Character encoding (e.g., "utf-8").baseURL: Base URL for resolving relative URLs.Returns: Promise<boolean>
waitForLoad(): Promise<boolean>Waits until the WebView has finished loading content.
Promise<boolean>getHTML(): Promise<string | null>Returns the current HTML content of the page.
Promise<string | null>evaluateJavaScript<T = any>(javascript: string): Promise<T>Evaluates the specified JavaScript string in the context of the WebView.
Parameters:
javascript: A JavaScript code string to be evaluated.
To retrieve a result from JavaScript, the code must explicitly use the return keyword.Returns: Promise<T> — Resolves with the result of the JavaScript evaluation. If the JavaScript code returns a value, it will be returned as the resolved value of the Promise.
Another example:
addScriptMessageHandler<P = any, R = any>(name: string, handler: (params?: P) => R): Promise<void>Installs a message handler callable from JavaScript in the web page, and enables sending a reply back from native code.
Parameters:
name: The message handler name. Must be unique and non-empty.handler: A callback function that receives parameters from JavaScript and returns a value. The return value will be sent back as the JavaScript promise resolution.Returns: Promise<void> — Resolves when the message handler is added successfully.
present(options?: { fullscreen?: boolean, navigationTitle?: string }): Promise<void>Presents the WebView in a modal sheet.
Options:
fullscreen: If true, displays the WebView in fullscreen mode.navigationTitle: Optional title for the navigation bar.Returns: Promise<void>
canGoBack(): Promise<boolean>Checks whether the WebView can go back in history.
canGoForward(): Promise<boolean>Checks whether the WebView can go forward in history.
goBack(): Promise<boolean>Navigates to the previous page in the history stack.
goForward(): Promise<boolean>Navigates to the next page in the history stack.
reload(): Promise<void>Reloads the current webpage.
dismiss(): voidDismisses the WebView if currently presented.
dispose(): voidDisposes the WebView instance and releases resources.